package com.xbongbong.util;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.enums.ShowTypeEnum;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/01/12 9:29
 */
public class ImportDataUtil {



    /**
     * 将旧数据的非导入字段的数据赋给新数据中，防止字段数据丢失
     * @param newData
     * @param oldData
     * @param attrList
     * @return
     */
    public static void setOldData(JSONObject newData, JSONObject oldData, List<String> attrList) {
        if (CollectionsUtil.isEmpty(attrList)) {
            return;
        }
        // oldData的旧值赋给newData里
        Set<Map.Entry<String, Object>> entrySet = oldData.entrySet();
        for (Map.Entry<String, Object> entry : entrySet) {
            Object value = entry.getValue();
            String key = entry.getKey();
            if (Objects.isNull(value) || attrList.contains(key) || Objects.nonNull(newData.get(key))) {
                continue;
            }
            // 将旧数据的非导入字段的数据赋给新数据中，防止字段数据丢失
            newData.put(key, value);
        }
    }

    /**
     * 如果自定义导入字段没包含所属部门字段，那么不能覆盖原有数据
     * @param oldDepartmentId
     * @param newDepartmentId
     * @param explainMap
     * @param attrList
     * @return
     */
    public static Long setOldDepartmentId(Long oldDepartmentId, Long newDepartmentId, Map<String, FieldAttrEntity> explainMap, List<String> attrList) {
        if (explainMap.containsKey(FieldTypeEnum.DEPARTMENTID.getAlias()) && !attrList.contains(FieldTypeEnum.DEPARTMENTID.getAlias())) {
            return oldDepartmentId;
        }
        return newDepartmentId;
    }

}
